<template>
  <div>
    <el-dialog v-dialogDrag title="新增通知公告" :visible="addShow" width="40%" @close="dialogClose"
      :close-on-click-modal="false">
      <el-form ref="addForm" :model="formFileds" :rules="rules" label-width="auto">
        <el-form-item label="公告名称：" prop="name">
          <el-input type="text" v-model="formFileds.name"></el-input>
        </el-form-item>
        <el-form-item label="公告类型：" prop="type">
          <el-select v-model="formFileds.type" placeholder="请选择">
            <!-- <el-option v-for="item in NoticeTypeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option> -->
            <el-option v-if="user=='系统管理员'" label="通知公告" value="通知公告">
            </el-option>
            <el-option v-if="user=='系统管理员'" label="学术活动" value="学术活动">
            </el-option>
            <el-option v-if="user=='企业管理员'" label="内部公告" value="内部公告">
            </el-option>
            <el-option v-if="user=='企业管理员'" label="招标公告" value="招标公告">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告内容：" prop="content">
          <el-input type="textarea" :rows="3" v-model="formFileds.content"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()" class="pull-right margin-l-25">确定
          </el-button>
          <el-button @click="dialogClose" class="pull-right">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addNotice } from "@/api/systemManage/noticeManage";
import { findByName } from "@/api/systemManage/dataDictionary";
export default {
  //接受父组件
  props: {
    addShow: {
      type: Boolean,
    },
  },

  data () {
    return {
      user:localStorage.getItem('role'),
      NoticeTypeOptions: [],
      formFileds: {
        name: "",
        content: "",
        type: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入公告名称",
            trigger: "change,blur",
          },
        ],
        content: [
          {
            required: false,
            message: "请输入公告内容",
            trigger: "change,blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择公告类型",
            trigger: "change,blur",
          },
        ],
      },
    };
  },
  methods: {
    dialogClose () {
      this.$emit("dialogClose");
    },
    submitForm () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addNotice(this.formFileds).then((res) => {
            this.$emit("close");
            this.$emit("dialogClose");
          });
        } else {
          return;
        }
      });
    },
    getType () {
      findByName("input=" + "公告类型").then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].dictName == '公告类型')
            this.NoticeTypeOptions.push({
              label: res.data[i].dictValue,
              value: res.data[i].dictValue,
            });
        }
      });
    },
  },
  mounted () {
    this.getType();
  },
};
</script>

<style lang="less" scoped></style>
